<?php

class ControllerProductCompare extends Controller {

    public function index() {
        $this->language->load('product/compare');

        $this->load->model('catalog/product');

        $this->load->model('tool/image');

        if (!isset($this->session->data['compare'])) {
            $this->session->data['compare'] = array();
        }
        if (isset($this->request->get['remove'])) {
            if ($this->request->get['remove'] == 'removeall') {
                // foreach ($this->session->data['compare'] as $product_id) {
                unset($this->session->data['compare']);
                //  }
            }
        }

        if (isset($this->request->post['remove'])) {
            $key = array_search($this->request->post['remove'], $this->session->data['compare']);

            if ($key !== false) {
                unset($this->session->data['compare'][$key]);
            }

            $this->redirect($this->url->link('product/compare'));
        }

        $this->document->setTitle($this->language->get('heading_title'));

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home'),
            'separator' => false
        );

        $url = '';

        if (isset($this->request->get['compare'])) {
            $url .= 'compare=' . $this->request->get['compare'];
        }

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('product/compare', $url),
            'separator' => $this->language->get('text_separator')
        );

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_product'] = $this->language->get('text_product');
        $this->data['text_name'] = $this->language->get('text_name');
        $this->data['text_image'] = $this->language->get('text_image');
        $this->data['text_price'] = $this->language->get('text_price');
        $this->data['text_model'] = $this->language->get('text_model');
        $this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
        $this->data['text_availability'] = $this->language->get('text_availability');
        $this->data['text_rating'] = $this->language->get('text_rating');
        $this->data['text_summary'] = $this->language->get('text_summary');
        $this->data['text_weight'] = $this->language->get('text_weight');
        $this->data['text_dimension'] = $this->language->get('text_dimension');
        $this->data['text_remove'] = $this->language->get('text_remove');
        $this->data['text_empty'] = $this->language->get('text_empty');

        $this->data['button_cart'] = $this->language->get('button_cart');
        $this->data['button_continue'] = $this->language->get('button_continue');

        $this->data['action'] = $this->url->link('product/compare');

        $this->data['products'] = array();
        $this->data['attribute_groups'] = array();
        if (!empty($this->session->data['compare'])) {
            foreach ($this->session->data['compare'] as $product_id) {
                $product_info = $this->model_catalog_product->getProduct($product_id);

                if ($product_info) {
                    if ($product_info['image']) {
                        $image = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_compare_width'), $this->config->get('config_image_compare_height'));
                    } else {
                        $image = false;
                    }

                    if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
                        $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                    } else {
                        $price = false;
                    }

                    if ((float) $product_info['special']) {
                        $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                    } else {
                        $special = false;
                    }

                    if ($product_info['quantity'] <= 0) {
                        $availability = $product_info['stock_status'];
                    } elseif ($this->config->get('config_stock_display')) {
                        $availability = $product_info['quantity'];
                    } else {
                        $availability = $this->language->get('text_instock');
                    }

                    $attribute_data = array();

                    $attribute_groups = $this->model_catalog_product->getProductAttributes($product_id);

                    foreach ($attribute_groups as $attribute_group) {
                        foreach ($attribute_group['attribute'] as $attribute) {
                            $attribute_data[$attribute['attribute_id']] = $attribute['text'];
                        }
                    }

                    $this->data['products'][$product_id] = array(
                        'product_id' => $product_info['product_id'],
                        'name' => $product_info['name'],
                        'thumb' => $image,
                        'price' => $price,
                        'special' => $special,
                        'description' => substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, 200) . '..',
                        'model' => $product_info['model'],
                        'manufacturer' => $product_info['manufacturer'],
                        'availability' => $availability,
                        'rating' => (int) $product_info['rating'],
                        'reviews' => sprintf($this->language->get('text_reviews'), (int) $product_info['reviews']),
                        'weight' => $this->weight->format($product_info['weight'], $product_info['weight_class_id']),
                        'length' => $this->length->format($product_info['length'], $product_info['length_class_id']),
                        'width' => $this->length->format($product_info['width'], $product_info['length_class_id']),
                        'height' => $this->length->format($product_info['height'], $product_info['length_class_id']),
                        'attribute' => $attribute_data,
                        'href' => $this->url->link('product/product', 'product_id=' . $product_id),
                        'remove' => $this->url->link('product/product', 'product_id=' . $product_id)
                    );

                    foreach ($attribute_groups as $attribute_group) {
                        $this->data['attribute_groups'][$attribute_group['attribute_group_id']]['name'] = $attribute_group['name'];

                        foreach ($attribute_group['attribute'] as $attribute) {
                            $this->data['attribute_groups'][$attribute_group['attribute_group_id']]['attribute'][$attribute['attribute_id']]['name'] = $attribute['name'];
                        }
                    }
                }
            }
        } else {
            $this->redirect($this->url->link('common/home'));
        }
        $this->data['continue'] = $this->url->link('common/home');

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/compare.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/product/compare.tpl';
        } else {
            $this->template = 'default/template/product/compare.tpl';
        }

        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render());
    }

    public function update() {
        $this->language->load('product/compare');

        $json = array();

        if (!isset($this->session->data['compare'])) {
            $this->session->data['compare'] = array();
        }

        if (isset($this->request->post['product_id'])) {
            $product_id = $this->request->post['product_id'];
        } else {
            $product_id = 0;
        }

        $this->load->model('catalog/product');

        $product_info = $this->model_catalog_product->getProduct($product_id);

        if ($product_info) {
            if (!in_array($this->request->post['product_id'], $this->session->data['compare'])) {
                if (count($this->session->data['compare']) > 4) {
                    array_shift($this->session->data['compare']);
                }

                $this->session->data['compare'][] = $this->request->post['product_id'];
            }
           $json['href']= $this->url->link('product/product', 'product_id=' . $product_info['product_id']);
           $json['name']=$product_info['name'];             
           $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('product/compare'));
            
            $json['total'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
        }

        $this->response->setOutput(json_encode($json));
    }

    public function removeupdate() {
        $this->language->load('product/compare');

        $json = array();

        if (!isset($this->session->data['compare'])) {
            $this->session->data['compare'] = array();
        }

        if (isset($this->request->post['product_id'])) {
            $product_id = $this->request->post['product_id'];
        } else {
            $product_id = 0;
        }

        $this->load->model('catalog/product');

        $product_info = $this->model_catalog_product->getProduct($product_id);

        if ($product_info) {
            if (in_array($this->request->post['product_id'], $this->session->data['compare'])) {
                $key = array_search($this->request->post['product_id'], $this->session->data['compare']);

                if ($key !== false) {
                    unset($this->session->data['compare'][$key]);
                }
            }

            $json['success'] = sprintf($this->language->get('text_success_remove'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('product/compare'));
            if ($this->session->data['compare'] > 1) {
                $total = count($this->session->data['compare']) - 1;
            } else {
                $total = 0;
            }
            $json['total_pro'] = count($this->session->data['compare']);
            $json['total'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? $total : 0));
        }

        $this->response->setOutput(json_encode($json));
    }

    public function removeallupdate() {
        $this->language->load('product/compare');

        $json = array();

        if (!isset($this->session->data['compare'])) {
            $this->session->data['compare'] = array();
        }

        if (isset($this->request->post['product'])) {
            if ($this->request->post['product'] == 'removeall') {
                $this->load->model('catalog/product');


                $this->load->model('catalog/product');
                unset($this->session->data['compare']);
                $total = 0;
                $json['success'] = sprintf($this->language->get('text_success_remove_all'));

                $json['total_pro'] = $total;
                $json['total'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? $total : 0));
            }
        }
        $this->response->setOutput(json_encode($json));
    }

}
?>